<template>
<div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
       <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="isFlag">
            <el-checkbox v-model="form.isFlag" label="我已阅读并同意用户协议和隐私条款"></el-checkbox>
        </el-form-item>
        <el-form-item>
           <el-button style="width:100%" :loading="isLoading" type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      isLoading: false,
      form: {
        mobile: '13911111111',
        code: '246810',
        isFlag: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机格式', trigger: 'blur' }
        ],
        code: [ // pattern正则验证式
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '数字格式不正确', trigger: 'change' }
        ],
        isFlag: [ // 需要自定义进行对true绑定
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请阅读并同意协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async login () {
      try {
        // 根据返回结果：
        const res = await this.$http.post('mp/v1_0/authorizations', { mobile: this.form.mobile, code: this.form.code })
        this.$message({ message: '登录成功', type: 'success' })
        this.isLoading = false
        // 登录成功把token保存到本地·
        localStorage.setItem('tokenInfo', JSON.stringify(res.data.data.token))
        // （1）成功 -> 跳转
        this.$router.push({ path: '/' })
        console.log(res)
      } catch (err) {
        //  (2) 失败 -> 提示
        this.$message({ message: '登录失败', type: 'error' })
        this.isLoading = false
        console.log(err)
      }
    },
    onSubmit () {
      // 表单整体校验
      this.$refs.form.validate(result => {
        // 验证通过
        if (result) {
          this.isLoading = true // 进行页面跳转的优化防止用户多次点击
          // result是个布尔值 true和false
          const token = localStorage.getItem('tokenInfo')
          if (token) {
            // 如果本地有token 则清除token再进行登录
            localStorage.removeItem('tokenInfo')
          }
          this.login()
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
// 铺满满屏
.login-container{
  position: fixed;
  left: 0;
  top:0;
  right: 0;
  bottom: 0;
  // left: 0 top:0 right: 0 bottom: 0是铺满全屏
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
  .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
